<?php

namespace App\Http\Controllers\Entity\Asset\Audio;

use App\Admin\Extensions\Tools\GridView;
use App\Http\Controllers\Controller;
use App\Http\Requests\Entity\Asset\IaassetRequest;
use App\Models\Common\CatData\Video\Vcat;
use App\Models\Common\EntityData\Channel\Cp;
use App\Models\Common\EntityData\Channel\Sp;
use App\Models\Common\EntityData\Video\Actor;
use App\Models\Common\EntityData\Video\Director;
use App\Models\Common\Enumdata\Enumop\Video\Varea;
use App\Models\Common\Enumdata\Enumop\Video\Vlang;
use App\Models\Common\Enumdata\Enumop\Video\Vyear;
use App\Models\Common\TagData\Video\Vcattag;
use App\Models\Entity\Asset\Video\Media\Vifile;
use App\Models\Entity\Asset\Video\Media\Vposteri;
use App\Models\Entity\Asset\Iaasset;
use App\Models\Entity\Asset\Video\VassetiVcattag;
use App\Models\Entity\Asset\Video\Vseriali;
use App\Models\Entity\content_audit\content_audit;
use App\Repositories\Entity\Asset\IaassetRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Excel;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;
use Validator;

class IaassetController extends Controller
{
    use ModelForm;

    /** @var  IaassetRepository */
    private $iaassetRepository;

    protected $model;

    public function __construct(IaassetRepository $iaassetRepo)
    {
        $this->model             = new Iaasset;
        $this->iaassetRepository = $iaassetRepo;
    }

    /**
     * Display a listing of the Iaasset.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        //Admin::script('injectIaasset();');
        return Admin::content(function (Content $content) use ($request) {
            $content->header('音频总集');
            switch ($request->get('view')){
                case 'editor':
                {
                    $content->description('编辑审核申请');
                    $content->body($this->editor_grid($request));
                    break;
                }
                case 'online':
                {
                    $content->description('上线申请');
                    $content->body($this->online_grid($request));
                    break;
                }
                case 'publish':
                {
                    $content->description('发布申请');
                    $content->body($this->publish_grid($request));
                    break;
                }
                case 'offline':
                {
                    $content->description('下线申请');
                    $content->body($this->offline_grid($request));
                    break;
                }
                default:
                    $content->description(__('entity.asset.audio.iaassets.index_description'));
                    $content->body($this->grid($request));
                    $content->row(view('entity.tag.addContentTagModal'));
                    break;
            }

        });
    }

    /**
     * Show the form for creating a new Iaasset.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.audio.iaassets.create_header'));
            $content->description(__('entity.asset.audio.iaassets.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Iaasset in storage.
     *
     * @param IaassetRequest $request
     *
     * @return Response
     */
    public function store(IaassetRequest $request)
    {
        $input   = $request->all();
        $iaasset = $this->iaassetRepository->create($input);
        if ($iaasset) {
            // 手动添加-中间表
            if (is_array($request->get('sps'))) {
                $iaasset->sps()->sync(array_filter($request->get('sps')));
            }
            if (is_array($request->get('cps'))) {
                $iaasset->cps()->sync(array_filter($request->get('cps')));
            }
            if (is_array($request->get('directors'))) {
                $iaasset->directors()->sync(array_filter($request->get('directors')));
            }
            if (is_array($request->get('actors'))) {
                $iaasset->actors()->sync(array_filter($request->get('actors')));
            }
            if (is_array($request->get('vifiles'))) {
                $iaasset->vifiles()->sync(array_filter($request->get('vifiles')));
            }
            if (is_array($request->get('vposteris'))) {
                $iaasset->vposteris()->sync(array_filter($request->get('vposteris')));
            }
            if (is_array($request->get('vyears'))) {
                $iaasset->vyears()->sync(array_filter($request->get('vyears')));
            }
            if (is_array($request->get('vlangs'))) {
                $iaasset->vlangs()->sync(array_filter($request->get('vlangs')));
            }
            if (is_array($request->get('vareas'))) {
                $iaasset->vareas()->sync(array_filter($request->get('vareas')));
            }
        }

        Flash::success(__('entity.asset.audio.iaassets.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.audio.iaassets.index'));
    }

    /**
     * Display the specified Iaasset.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $iaasset = $this->iaassetRepository->findWithoutFail($id);

        if (empty($iaasset)) {
            Flash::error(__('entity.asset.audio.iaassets.not_found'));

            return redirect(route('entity.asset.audio.iaassets.index'));
        }

        return view('entity.asset.audio.iaassets.show')->with('iaasset', $iaasset);

    }

    /**
     * Show the form for editing the specified Iaasset.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.audio.iaassets.edit_header'));
            $content->description(__('entity.asset.audio.iaassets.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Iaasset in storage.
     *
     * @param  int              $id
     * @param IaassetRequest $request
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        $sValidators = [
            'iaasset_no'  => 'required|max:100|unique:iaassets,iaasset_no,' . $id,
            'name'        => 'required|max:100',
            'title'       => 'max:100',
            'subtitle'    => 'max:100',
            'score'       => 'numeric',
            // 'season'      => 'numeric',
            'episode'     => 'numeric',
            'serials'     => 'numeric',
            'vcat_id'     => 'required|max:10',
            'short_desc'  => 'required',
            'charge_type' => 'required',
            'listorder'   => 'required|numeric',
        ];
        $validators = [];

        $input = $request->all();

        // 判断是否行内编辑,只有下线的可以编辑
        $status = Iaasset::where(['online_status' => 0, 'id' => $id])->first();
        if ($status) {
            $input = $this->handleEditable($input);
        }

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }

        // 定义校验, 没有移到request
        $validator = Validator::make($input, $validators, [
            // 'required' => ':attribute 不能为空',
        ], [
            'iaasset_no' => '剧集编号',
            'name'       => '名称',
            'title'      => '标题',
            'subtitle'   => '副标题',
            'score'      => '评分',
            // 'season'     => '第几季',
            'episode'    => '集数',
            'serials'    => '集数',
            'vcat_id'    => '分类',
            'short_desc' => '描述',
            'listorder'  => '排序',
        ]
        );

        // 校验
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }

        // 排序
        if ($this->handleOrderable($id, $input)) {
            return response([
                'status'  => true,
                'message' => trans('admin::lang.update_succeeded'),
            ]);
        }

        $iaasset = $this->iaassetRepository->findWithoutFail($id);
        if (empty($iaasset)) {
            Flash::error(__('entity.asset.audio.iaassets.not_found'));

            return redirect(route('entity.asset.audio.iaassets.index'));
        }
        $iaasset = $this->iaassetRepository->update($input, $id);

        if ($iaasset) {
            // 手动添加-中间表
            if (is_array($request->get('sps'))) {
                $iaasset->sps()->sync(array_filter($request->get('sps')));
            }
            if (is_array($request->get('cps'))) {
                $iaasset->cps()->sync(array_filter($request->get('cps')));
            }
            if (is_array($request->get('directors'))) {
                $iaasset->directors()->sync(array_filter($request->get('directors')));
            }
            if (is_array($request->get('actors'))) {
                $iaasset->actors()->sync(array_filter($request->get('actors')));
            }
            if (is_array($request->get('vifiles'))) {
                $iaasset->vifiles()->sync(array_filter($request->get('vifiles')));
            }
            if (is_array($request->get('vposteris'))) {
                $iaasset->vposteris()->sync(array_filter($request->get('vposteris')));
            }
            if (is_array($request->get('vyears'))) {
                $iaasset->vyears()->sync(array_filter($request->get('vyears')));
            }
            if (is_array($request->get('vlangs'))) {
                $iaasset->vlangs()->sync(array_filter($request->get('vlangs')));
            }
            if (is_array($request->get('vareas'))) {
                $iaasset->vareas()->sync(array_filter($request->get('vareas')));
            }
            if (is_array($request->get('vcattags'))) {
                $iaasset->vcattags()->sync(array_filter($request->get('vcattags')));
            }
        }

        Flash::success(__('entity.asset.audio.iaassets.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.audio.iaassets.index'));
    }

    /**
     * Remove the specified Iaasset from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        } else {
            // 只有下线且下线发布或上线未发布的才能删除
            $iaasset = Iaasset::where('id', $id)->where(['publish_down_status' => 1])->orWhere(['online_status' => 1, 'publish_status' => 0])->first();
            if (empty($iaasset)) {
                return response()->json(['message' => '只有状态为下线且已下线发布才可以删除', 'status' => false]);
            }
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            // 只有下线且下线发布的才能删除
            $id = Iaasset::where(['online_status' => 0, 'publish_down_status' => 1])->whereIn('id', $id)->pluck('id')->toArray();
            if ($flag = $this->iaassetRepository->batchDelete('id', $id)) {
                $iaasset->sps()->detach();

                $iaasset->cps()->detach();

                $iaasset->directors()->detach();

                $iaasset->actors()->detach();
                $iaasset->vifiles()->detach();

                $iaasset->vposteris()->detach();

                $iaasset->vyears()->detach();

                $iaasset->vlangs()->detach();

                $iaasset->vareas()->detach();
                return response()->json(['message' => __('entity.asset.audio.iaassets.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => '只有状态为下线发布的才可以删除', 'status' => $flag]);
            }
        }

        $iaasset = $this->iaassetRepository->findWithoutFail($id);

        if (empty($iaasset)) {
            Flash::error(__('entity.asset.audio.iaassets.not_found'));

            return redirect(route('entity.asset.audio.iaassets.index'));
        }

        if ($flag = $this->iaassetRepository->delete($id)) {

            $iaasset->sps()->detach();

            $iaasset->cps()->detach();

            $iaasset->directors()->detach();

            $iaasset->actors()->detach();
            $iaasset->vifiles()->detach();

            $iaasset->vposteris()->detach();

            $iaasset->vyears()->detach();

            $iaasset->vlangs()->detach();

            $iaasset->vareas()->detach();
            return response()->json(['message' => __('entity.asset.audio.iaassets.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }
    // 剧集详情
    public function iaassetDetails($id)
    {

        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('剧集详情'));

            // if (Request::get('view') !== 'table') {
            //     $grid->view('admin.grid.card');
            // }

            // 剧集详情
            $data = $this->assetDetailsData($id);
            $content->row(view('entity.asset.audio.iaasset.details', $data));

            // 剧集详情
            // $content->row($this->assetDetailsBox($id));

            // 分集数据
            $content->row($this->iaassetTab($id));
            // $content->body($this->iaassetGrid($id));
        });
    }

    // 剧集详情的数据
    public function assetDetailsData($id)
    {
        $iaassetDetails = Iaasset::where('id', '=', $id)->with('sps', 'cps', 'directors', 'actors', 'vlangs', 'vareas', 'vyears', 'vcattags')->first();

        // 内容商
        $cps = '';
        if ($cps = $iaassetDetails->cps) {
            $cps = $iaassetDetails->cps->pluck('name')->implode(',');
        }
        // 服务商
        $sps = '';
        if ($sps = $iaassetDetails->sps) {
            $sps = $iaassetDetails->sps->pluck('name')->implode(',');
        }
        // 演员
        $actors = '';
        if ($actors = $iaassetDetails->actors) {
            $actors = $iaassetDetails->actors->pluck('name')->implode(',');
        }
        // 导演
        $directors = '';
        if ($directors = $iaassetDetails->directors) {
            $directors = $iaassetDetails->directors->pluck('name')->implode(',');
        }
        // 语言
        $vlangs = '';
        if ($vlangs = $iaassetDetails->vlangs) {
            $vlangs = $iaassetDetails->vlangs->pluck('name')->implode(',');
        }
        // 年代
        $vyears = '';
        if ($vyears = $iaassetDetails->vyears) {
            $vyears = $iaassetDetails->vyears->pluck('name')->implode(',');
        }
        // 地区
        $vareas = '';
        if ($vareas = $iaassetDetails->vareas) {
            $vareas = $iaassetDetails->vareas->pluck('name')->implode(',');
        }
        // 归类
        $vcat = '';
        if ($vcat = $iaassetDetails->vcat) {
            $vcat = $iaassetDetails->vcat->name;
        }
        // 分类
        $vcattags = '';
        if ($vcattags = $iaassetDetails->vcattags) {
            $vcattags = $iaassetDetails->vcattags->pluck('name')->implode(',');
        }

        $data['name']       = $iaassetDetails->name;
        $data['iaasset_no'] = $iaassetDetails->iaasset_no;
        $data['title']      = $iaassetDetails->title;
        $data['subtitle']   = $iaassetDetails->subtitle;
        $data['score']      = $iaassetDetails->score;
        $data['episode']    = $iaassetDetails->episode;
        $data['season']     = $iaassetDetails->season;
        $data['tags']       = $vcattags;
        $data['vcat']       = $vcat;
        $data['short_desc'] = $iaassetDetails->short_desc;
        $data['long_desc']  = $iaassetDetails->long_desc;
        $data['directors']  = $directors;
        $data['actors']     = $actors;
        $data['cps']        = $cps;
        $data['sps']        = $sps;
        $data['vlangs']     = $vlangs;
        $data['vyears']     = $vyears;
        $data['vareas']     = $vareas;
        $data['firstdate']  = date('Y-m-d', strtotime($iaassetDetails->firstdate)); // $iaassetDetails->firstdate
        $data['online_at']  = date('Y-m-d', strtotime($iaassetDetails->online_at)); // $iaassetDetails->online_at
        $data['offline_at'] = date('Y-m-d', strtotime($iaassetDetails->offline_at)); // $iaassetDetails->offline_at;

        return $data;
    }

    // 剧集tab
    public function iaassetTab($id)
    {
        $tab = new Tab();
        // if ($iaasset = Iaasset::has('vserialis')->find($id)) {
        // $iaasset = Iaasset::where(['id' => $id, 'iaasset_type' => 1])->first();
        if (Iaasset::where(['id' => $id, 'iaasset_type' => 1])->first() || Iaasset::has('iaserialis')->find($id)) {
            $tab->add('图片信息', $this->vposteriGrid($id));
            $tab->add('分集信息', $this->iaassetGrid($id));
        } else {
            $tab->add('图片信息', $this->vposteriGrid($id));
            $tab->add('文件信息', $this->vifileGrid($id));
        }
        return $tab;
    }

    // 文件信息
    public function vifileGrid($id)
    {
        return Admin::grid(Vifile::class, function (Grid $grid) use ($id) {

            // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
            $data = Iaasset::where('id', '=', $id)->select('online_status', 'publish_status')->first();

            if ($data && $data->online_status == 1 && $data->publish_status == 1) {
                // 禁用行操作列
                $grid->disableActions();
                // 禁用创建按钮
                $grid->disableCreation();
            }

            // 文件数据
            $vifiles = Iaasset::find($id)->vifiles->toArray();
            $grid->model()->whereIn('id', array_column($vifiles, 'id'));
            $grid->column('id', 'ID')->sortable();
            $grid->column('sp.name', '服务商')->sortable();
            $grid->column('resolution.name', '清晰度')->sortable();
            $grid->column('vfileext.name', '文件扩展名')->sortable();
            $grid->column('title', '标题')->editable();
            $grid->column('subtitle', '副标题')->sortable();
            $grid->column('name', '名称')->editable();
            $grid->column('alias_name', '别名')->editable();
            $grid->column('path', '文件路径')->sortable();
            $grid->column('size', '文件大小(M)')->sortable();
            $grid->column('md5', '文件MD5')->sortable();
            $grid->column('duration', '时长')->sortable();
            $grid->column('href', '下载路径')->sortable();
            $grid->column('type', '文件类型')->display(function ($val) {
                switch ($val) {
                    case 1:
                        return '正片';
                        break;
                    case 2:
                        return '片花';
                        break;
                    case 3:
                        return '预告';
                        break;
                    default:
                        return '其他';
                        break;
                }
            });
            $grid->column('short_desc', '简述')->editable();
            $grid->column('listorder', '排序')->sortable()->editable();

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    $batch->disableDelete();
                });
            });

            // 修改新建, 编辑, 删除按钮的路径
            // $grid->resource(route('entity.asset.video.media.vifiles.index'));
            $grid->resource(route('entity.asset.video.media.vifiles.vasseti.index', ['iaasset_id' => $id]));

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();

            });
        }, 'vifile');
    }

    // 图片信息
    public function vposteriGrid($id)
    {
        return Admin::grid(Vposteri::class, function (Grid $grid) use ($id) {

            $uploadConfig = config('custom.base.upload.vasseti');
            $driver       = $uploadConfig['driver'];
            $dir          = $uploadConfig['dir'];
            $uploadHosts  = config('custom.base.host');
            $host         = $uploadHosts[$driver];

            // 图片数据
            $vposteris = Iaasset::find($id)->vposteris->toArray();
            $grid->model()->whereIn('id', array_column($vposteris, 'id'));
            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '名称')->editable();
            $grid->column('title', '显示标题')->editable();
            $grid->column('subtitle', '显示副标题')->sortable();
            $grid->column('vimgtag.name', '图片类型')->sortable();
            $grid->column('imgtpl.name', '图像规格')->sortable();
            $grid->column('href', '海报链接')->sortable();
            $grid->column('listorder', '排序')->sortable()->editable();

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    $batch->disableDelete();
                });
            });

            // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
            $data = Iaasset::where('id', '=', $id)->select('online_status', 'publish_status')->first();

            if ($data && $data->online_status == 1 && $data->publish_status == 1) {
                // 禁用行操作列
                $grid->disableActions();
                // 禁用创建按钮
                $grid->disableCreation();

                $flag = false;

            } else {

                $flag = true;

            }

            // 修改新建, 编辑, 删除按钮的路径
            // $grid->resource(route('entity.asset.video.media.vposteris.index'));
            $grid->resource(route('entity.asset.video.media.vposteris.vasseti.index', ['iaasset_id' => $id]));

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
            });

            $grid->tools(function ($tools) {
                $tools->append(new GridView());
            });

            if (\Illuminate\Support\Facades\Request::get('view') !== 'table') {
                $grid->setView('admin.grid.card', ['image_column' => 'href', 'text_column' => 'title', 'server' => $host, 'flag' => $flag]);
            }
        }, 'vposteri');
    }

    // 分集信息
    public function iaassetGrid($id)
    {
        return Admin::grid(Vseriali::class, function (Grid $grid) use ($id) {

            // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
            $data = Iaasset::where('id', '=', $id)->select('online_status', 'publish_status')->first();

            if ($data && $data->online_status == 1 && $data->publish_status == 1) {
                // 禁用行操作列
                $grid->disableActions();
                // 禁用创建按钮
                $grid->disableCreation();
            }

            // 分集数据
            $grid->model()->where('iaasset_id', $id);
            $grid->column('id', 'ID')->sortable();
            $grid->column('vseriali_no', '分集编号');
            $grid->column('name', '分集名称')->editable();
            $grid->column('title', '标题')->editable();
            $grid->column('episode', '第几集')->editable();
            $grid->column('score', '分集评分')->editable();
            $grid->column('firstdate', '首播日期')->display(function ($val) {
                if ($val) {
                    return date('Y-m-d', strtotime($val));
                }
                return '';
            })->sortable();
            $grid->column('short_desc', '简介')->editable();
            $grid->column('listorder', '排序')->sortable()->editable();
            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    $batch->disableDelete();
                });
            });

            $grid->actions(function ($actions) {
                // 编辑按钮
                // $actions->disableEdit();
                // 删除按钮
                // $actions->disableDelete();
            });

            // 修改新建, 编辑, 删除按钮的路径
            // $grid->resource(route('entity.asset.video.vserialis.index'));
            $grid->resource(route('entity.asset.video.vserialis.iaasset.index', ['iaasset_id' => $id]));

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
            });
        }, 'iaasset');

    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(Iaasset::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('iaasset_no', '编号')->help('必填');
            $form->text('name', '名称')->help('必填');
            $form->text('title', '标题');
            $form->text('subtitle', '副标题');
            $form->select('iaasset_type', '剧集类型')->options(array(0 => '单集', 1 => '分集总集'))->default(0);
            $form->number('score', '评分');
            $form->number('season', '第几季');
            $form->number('serials', '总集数');
            $form->number('upto', '更新到');
            $vcats = Vcat::pluck('name', 'id')->toArray();
            $form->select('vcat_id', '归类')->options(['' => '选择'] + $vcats)->load('vcattags', route('common.cat_data.video.vcats.vcattag_or_empty'), 'id', 'name')->help('必填');
            $vcattags = Vcattag::pluck('name', 'id')->toArray();
            // $form->multipleSelect('vcattags', '分类')->options($vcattags);
            $form->multipleSelect('vcattags', '分类')->options(['' => '选择'] + $vcattags);
            $vyears = Vyear::pluck('name', 'id');
            $form->multipleSelect('vyears', '年代')->options($vyears);
            $vareas = Varea::pluck('name', 'id');
            $form->multipleSelect('vareas', '地区')->options($vareas);
            $vlangs = Vlang::pluck('name', 'id');
            $form->multipleSelect('vlangs', '语言')->options($vlangs);
            $vifiles = Vifile::pluck('name', 'id');
            $form->multipleSelect('vifiles', '资源文件')->options($vifiles);
            $vposteris = Vposteri::pluck('name', 'id');
            $form->multipleSelect('vposteris', '图片')->options($vposteris);
            // 新增分集
            // $form->hasMany('vserialis', '分集', function (Form\NestedForm $form) {
            //     $vserialis = Vseriali::pluck('name', 'id')->toArray();
            //     // 分集名称
            //     $form->select('vserialis', '名称')->options($vserialis);
            // });
            $form->date('firstdate', '首播日期')->default(date('Y-m-d',time()));
            $form->date('online_at', '上线日期')->default(date('Y-m-d',time()));
            $form->date('offline_at', '下线日期')->default(date('Y-m-d',time()));
            $form->textarea('short_desc', '简介')->help('必填');
            $form->textarea('long_desc', '详情');
            $sps = Sp::pluck('name', 'id');
            $form->multipleSelect('sps', '服务商(SP)')->options($sps);
            $cps = Cp::pluck('name', 'id');
            $form->multipleSelect('cps', '内容商(CP)')->options($cps);
            $directors = Director::pluck('name', 'id');
            $form->multipleSelect('directors', '导演')->options($directors);
            $actors = Actor::pluck('name', 'id');
            $form->multipleSelect('actors', '演员')->options($actors);
            $form->select('charge_type', '收费类型')->options(array(
                0 => '免费',
                1 => '收费',
            ));
            $form->number('listorder', '排序');

            // $form->display('created_at', __('base.created_at'));
            // $form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid(Request $request)
    {
        return Admin::grid(Iaasset::class, function (Grid $grid) use ($request) {

            // 状态过滤
             //Publication::statusFilter($request->input('status'), $request->input('published'), $grid);

            // 表格中的按钮
            $grid->actions(function ($actions) {
                // 表格中的按钮, 新增剧集详情
                // $actions->disableDelete();
                // $actions->disableEdit();
                // $publish_status      = $actions->row->publish_status;
                // $publish_down_status = $actions->row->publish_down_status;
                // $online_status       = $actions->row->online_status;
                $id                  = $actions->getKey();
                // 媒资编辑按钮
                // if ($publish_down_status === 1 && $online_status === 0 && Admin::user()->can('entity.asset.audio.iaassets.iaasset_details')) {
                $url = route('entity.asset.audio.iaassets.iaasset_details', ['iaasset_id' => $id]);
                $actions->prepend('<a href ="' . $url . '" title="剧集详情"><i class="fa fa-list"></i></a>');
                // }

                // // 上线按钮
                // if ($publish_down_status === 1 && $online_status === 0 && Admin::user()->can('entity.asset.audio.iaassets.up')) {
                //     $url      = route('entity.asset.audio.iaassets.up');
                //     $function = "upAndDownByOne('$url', 'post', {ids: [$id],action: 'up',_token: LA.token})";
                //     $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="上线"><i class="fa fa-arrow-up"></i></a>');
                // }
                // // 下线按钮
                // if ($online_status === 1 && Admin::user()->can('entity.asset.audio.iaassets.down')) {
                //     $url      = route('entity.asset.audio.iaassets.down');
                //     $function = "upAndDownByOne('$url', 'post', {ids: [$id],action: 'down',_token: LA.token})";
                //     $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="下线"><i class="fa fa-arrow-down"></i></a>');
                // }
                // // 上线发布按钮
                // if ($online_status === 1 && $publish_status === 0  && Admin::user()->can('entity.asset.audio.iaassets.publish_one')) {
                //     $url      = route('entity.asset.audio.iaassets.publish_one');
                //     $function = "upAndDownByOne('$url', 'post', {ids: [$id],action: 'publish',_token: LA.token})";
                //     $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="上线发布"><i class="fa fa-cloud-upload"></i></a>');
                // }
                // // 下线发布按钮
                // if ($online_status === 0 && $publish_down_status === 0 && Admin::user()->can('entity.asset.audio.iaassets.publish_down_one')) {
                //     $url      = route('entity.asset.audio.iaassets.publish_down_one');
                //     $function = "upAndDownByOne('$url', 'post', {ids: [$id],action: 'publishDown',_token: LA.token})";
                //     $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="下线发布"><i class="fa fa-cloud-download"></i></a>');
                // }
                //打标签
                $url = route('entity.tag.selectTag', ['tag_content_id' => $id,'tag_content_type'=>'iaasset']);
                $actions->append('<a href="'.$url.'" data-toggle="modal"  data-target="#add-content-tag-modal" title="打标签"><i class="fa fa-paper-plane"></i></a>');

                //导入栏目
                $url = route('application.column_assets.selectColumn', ['content_id' => $id,'content_type'=>'2']);
                $actions->append('<a href="'.$url.'" data-toggle="modal"  data-target="#add-content-tag-modal" title="导入栏目"><i class="fa fa-download"></i></a>');

                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.destroy')) {
                    $actions->disableDelete();
                }

            });

            //添加按钮
            if (Admin::user()->cannot('entity.asset.audio.iaassets.create')) {
                $grid->disableCreation();
            }

            //导出按钮
            if (Admin::user()->cannot('entity.asset.audio.iaassets.export')) {
                $grid->disableExport();
            }

            // 批量操作
            $grid->tools(function ($tools) {

                //$url2 = route('entity.asset.audio.iaassets.progress');
                // // 下线发布功能
                // if (Admin::user()->can('entity.asset.audio.iaassets.publish_down')) {
                //     $url      = route('entity.asset.audio.iaassets.publish_down');
                //     $function = "upAndDownByAll('$url', '$url2', 'get')";
                //     $tools->append('<a href="javascript:void(0)" onclick="' . $function . '" class="btn btn-sm btn-info pull-right" style="margin-right: 10px"> <i class="fa fa-cloud-download"></i>下线发布</a>');
                // }
                // // 上线发布功能
                // if (Admin::user()->can('entity.asset.audio.iaassets.publish')) {
                //     $url      = route('entity.asset.audio.iaassets.publish');
                //     $function = "upAndDownByAll('$url', '$url2', 'get')";
                //     $tools->append('<a href="javascript:void(0)" onclick="' . $function . '" class="btn btn-sm btn-info pull-right" style="margin-right: 10px"> <i class="fa fa-cloud-upload"></i>上线发布</a>');
                // }
                // // 全部下线功能
                // if (Admin::user()->can('entity.asset.audio.iaassets.all_down')) {
                //     $url      = route('entity.asset.audio.iaassets.all_down');
                //     $function = "upAndDownByAll('$url', '$url2', 'get')";
                //     $tools->append('<a href="javascript:void(0)" onclick="' . $function . '" class="btn btn-sm btn-danger pull-right" style="margin-right: 10px"> <i class="fa fa-arrow-down"></i>全部下线</a>');
                // }
                // // 全部上线功能
                // if (Admin::user()->can('entity.asset.audio.iaassets.all_up')) {
                //     $url      = route('entity.asset.audio.iaassets.all_up');
                //     $function = "upAndDownByAll('$url', '$url2', 'get')";
                //     $tools->append('<a href="javascript:void(0)" onclick="' . $function . '" class="btn btn-sm btn-success pull-right" style="margin-right: 10px"> <i class="fa fa-arrow-up"></i>全部上线</a>');
                // }
                // 过滤按钮
                //$tools->append(new \App\Admin\Extensions\Tools\Iaasset\IaassetStatus());
                //$tools->append(new \App\Admin\Extensions\Tools\Iaasset\IaassetPublished());
                // $tools->append(new \App\Admin\Extensions\Tools\Iaasset\IaassetAsyncResult());
                // 2017-11-22 add 清除缓存按钮
                // // entity.asset.audio.iaassets.clear_cache
                // if (Admin::user()->can('entity.asset.audio.iaassets.clear_cache')) {
                //     $tools->append('<a href=' . route('entity.asset.audio.iaassets.clear_cache') . ' class="btn btn-sm btn-warning pull-right" style="margin-right: 10px"><i class="fa fa-ban"></i> 清除缓存</a>');
                // }

                $url =  route('entity.assets.assets.model',['sp_no' => '','config_file_select'=>'']);
                $tools->append('<a href=' .$url. ' class="btn btn-sm btn-warning pull-right" title="导入数据"><i class="fa fa-ban"></i>导入数据</a>');


                // 批量按钮
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    if (Admin::user()->cannot('entity.asset.audio.iaassets.batch_destroy')) {
                        $batch->disableDelete();
                    }
                    // 批量上线按钮
                    // if (Admin::user()->can('entity.asset.audio.iaassets.up')) {
                    //     $batch->add('上线', new \App\Admin\Extensions\Tools\Iaasset\ReleaseIaasset('up'));
                    // }
                    // // 批量下线按钮
                    // if (Admin::user()->can('entity.asset.audio.iaassets.down')) {
                    //     $batch->add('下线', new \App\Admin\Extensions\Tools\Iaasset\ReleaseIaasset('down'));
                    // }

                    //批量打标签
                    $batch->add('打标签', new \App\Admin\Extensions\Tools\contentTag('iaasset'));

                    //批量导入栏目
                    $batch->add('导入栏目', new \App\Admin\Extensions\Tools\contentColumn('1'));
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('iaasset_no', '编号')->editable();
            $grid->column('name', '名称')->editable();
            $grid->column('iaasset_type', '剧集类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">单集</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-green">分集总集</span>';
                        break;
                }
            })->sortable();
            $grid->column('title', '标题')->editable();
            $grid->column('score', '评分')->editable()->label('default');
            $grid->column('serials', '集数')->editable();
            $grid->column('upto', '更新到')->editable();
            $grid->column('vcat.name', '归类')->label('info');
            $grid->vcattags('分类')->pluck('name')->label();
            $grid->tags('标签')->pluck('name')->label('primary');
            // $grid->vyears('年代')->pluck('name')->label('default');
            // $grid->vareas('地区')->pluck('name')->label('info');
            // $grid->vlangs('语言')->pluck('name')->label();
            // $grid->column('firstdate', '首播日期')->display(function ($val) {
            //     if ($val) {
            //         return date('Y-m-d', strtotime($val));
            //     }
            //     return '';
            // })->sortable();
            // $grid->vifiles('文件资源地址')->pluck('name')->label();
            // $grid->vposteris('图片')->pluck('name')->label();
            // $grid->column('short_desc', '简介')->sortable();
            // $grid->column('long_desc', '详情')->sortable();
            // $grid->sps('服务商(SP)')->pluck('name')->label();
            // $grid->cps('内容商(CP)')->pluck('name')->label();
            // $grid->directors('导演')->pluck('name')->label();
            // $grid->actors('演员')->pluck('name')->label();
            $grid->column('charge_type', '收费类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">免费</span>';
                        break;

                    case 1:
                        return '<span class="badge bg-red">收费</span>';
                        break;
                }
            });
            $grid->column('listorder', '排序')->sortable()->editable();
            // $grid->column('online_at', '上线日期')->display(function ($val) {
            //     if ($val) {
            //         return date('Y-m-d', strtotime($val));
            //     }
            //     return '';
            // })->sortable();
            // $grid->column('offline_at', '下线日期')->display(function ($val) {
            //     if ($val) {
            //         return date('Y-m-d', strtotime($val));
            //     }
            //     return '';
            // })->sortable();

            $grid->column('moderate_status', '编辑状态')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();

            $grid->column('online_status', '上线状态')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();
            $grid->column('publish_status', '发布状态')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();
            /**
             * 过滤
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();

                //评分
                $filter->between('score', '评分');

                // 归类
                $tempVcats = Vcat::pluck('name', 'id');
                $filter->equal('vcat_id', '归类')->select($tempVcats);

                // 语言
                $tempVlangs = Vlang::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vlangs', function ($query) use ($input) {
                        $query->where('vlang_id', '=', "{$input}");
                    });

                }, '语言')->select($tempVlangs);

                // 地区
                $tempVareas = Varea::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vareas', function ($query) use ($input) {
                        $query->where('varea_id', '=', "{$input}");
                    });

                }, '地区')->select($tempVareas);

                // 服务商(SP)
                $tempSps = Sp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('sps', function ($query) use ($input) {
                        $query->where('sp_id', '=', "{$input}");
                    });

                }, '服务商')->select($tempSps);

                // 内容商(CP)
                $tempCps = Cp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('cps', function ($query) use ($input) {
                        $query->where('cp_id', '=', "{$input}");
                    });

                }, '内容商')->select($tempCps);

                // 编号
                $filter->like('iaasset_no', '编号');

                // 标题
                $filter->like('title', '标题');

                // 分类
                $tempVcattags = Vcattag::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vcattags', function ($query) use ($input) {
                        $query->where('vcattag_id', 'like', "{$input}");
                    });
                }, '分类')->select($tempVcattags);

                // 演员
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('actors', function ($query) use ($input) {
                        $query->where('actors.name', 'like', "%{$input}%");
                    });
                }, '演员');

                // 导演
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('directors', function ($query) use ($input) {
                        $query->where('directors.name', 'like', "%{$input}%");
                    });
                }, '导演');

                // 首播时间
                $filter->between('firstdate', '首播时间')->datetime();
                // 上线时间
                $filter->between('online_at', '上线时间')->datetime();
                // 下线时间
                $filter->between('offline_at', '下线时间')->datetime();

            });
        });
    }

    protected function editor_grid(Request $request)
    {
        return Admin::grid(Iaasset::class, function (Grid $grid) use ($request) {

            // 状态过滤
            // Publication::statusFilter($request->input('status'), $request->input('published'), $grid);
            $grid->model()->where('moderate_status', '!=', 1);

            // 表格中的按钮
            $grid->actions(function ($actions) {

                $id                  = $actions->getKey();
                $url = route('entity.asset.audio.iaassets.iaasset_details', ['iaasset_id' => $id]);
                $actions->prepend('<a href ="' . $url . '" title="剧集详情"><i class="fa fa-list"></i></a>');

                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                //if (Admin::user()->cannot('entity.asset.audio.iaassets.destroy')) {
                    $actions->disableDelete();
                //}

                // 提交审核申请
                if ( Admin::user()->can('content_audit.content_audits.create.moderate')) {
                    $url      = route('entity.content_audit.content_audits.create.moderate');
                    $function = "upAndDownByOne('$url', 'post', {content_id: $id, content_model: 'iaasset',audit_type: 'moderate', _token: LA.token})";
                    $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="提交媒资审核申请"><i class="fa fa-eye"></i></a>');
                }

            });

            //添加按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.create')) {
                $grid->disableCreation();
            //}

            //导出按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.export')) {
                $grid->disableExport();
            //}

            // 批量操作
            $grid->tools(function ($tools) {
                // 批量按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('iaasset_no', '编号')->editable();
            $grid->column('name', '名称')->editable();
            $grid->column('iaasset_type', '剧集类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">单集</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-green">分集总集</span>';
                        break;
                }
            })->sortable();
            $grid->column('title', '标题')->editable();
            // $grid->column('subtitle', '副标题')->sortable();
            $grid->column('score', '评分')->editable()->label('default');
            $grid->column('season', '第几季')->editable();
            $grid->column('serials', '集数')->editable();
            $grid->column('upto', '更新到')->editable();
            $grid->column('vcat.name', '归类')->label('info');
            $grid->vcattags('分类')->pluck('name')->label();
            $grid->tags('标签')->pluck('name')->label('primary');
            $grid->vyears('年代')->pluck('name')->label('default');
            $grid->vareas('地区')->pluck('name')->label('info');
            $grid->vlangs('语言')->pluck('name')->label();
            $grid->column('firstdate', '首播日期')->display(function ($val) {
                if ($val) {
                    return date('Y-m-d', strtotime($val));
                }
                return '';
            })->sortable();
            // $grid->vifiles('文件资源地址')->pluck('name')->label();
            // $grid->vposteris('图片')->pluck('name')->label();
            // $grid->column('short_desc', '简介')->sortable();
            // $grid->column('long_desc', '详情')->sortable();
            // $grid->sps('服务商(SP)')->pluck('name')->label();
            // $grid->cps('内容商(CP)')->pluck('name')->label();
            // $grid->directors('导演')->pluck('name')->label();
            // $grid->actors('演员')->pluck('name')->label();
            $grid->column('charge_type', '收费类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">免费</span>';
                        break;

                    case 1:
                        return '<span class="badge bg-red">收费</span>';
                        break;
                }
            });
            $grid->column('listorder', '排序')->sortable()->editable();
            // $grid->column('online_at', '上线日期')->display(function ($val) {
            //     if ($val) {
            //         return date('Y-m-d', strtotime($val));
            //     }
            //     return '';
            // })->sortable();
            // $grid->column('offline_at', '下线日期')->display(function ($val) {
            //     if ($val) {
            //         return date('Y-m-d', strtotime($val));
            //     }
            //     return '';
            // })->sortable();
            //
            // $grid->column('online_status', '上线状态')->display(function ($val) {
            //     switch ($val) {
            //         case 0:
            //             return '<span class="badge bg-red">下线</span>';
            //             break;
            //         case 1:
            //             return '<span class="badge bg-green">上线</span>';
            //             break;
            //         default:
            //             return '未知';
            //     }
            // })->sortable();
            $grid->column('moderate_status', '编辑审核')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();

            /**
             * 过滤
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();

                //评分
                $filter->between('score', '评分');

                // 归类
                $tempVcats = Vcat::pluck('name', 'id');
                $filter->equal('vcat_id', '归类')->select($tempVcats);

                // 语言
                $tempVlangs = Vlang::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vlangs', function ($query) use ($input) {
                        $query->where('vlang_id', '=', "{$input}");
                    });

                }, '语言')->select($tempVlangs);

                // 地区
                $tempVareas = Varea::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vareas', function ($query) use ($input) {
                        $query->where('varea_id', '=', "{$input}");
                    });

                }, '地区')->select($tempVareas);

                // 服务商(SP)
                $tempSps = Sp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('sps', function ($query) use ($input) {
                        $query->where('sp_id', '=', "{$input}");
                    });

                }, '服务商')->select($tempSps);

                // 内容商(CP)
                $tempCps = Cp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('cps', function ($query) use ($input) {
                        $query->where('cp_id', '=', "{$input}");
                    });

                }, '内容商')->select($tempCps);

                // 编号
                $filter->like('iaasset_no', '编号');

                // 标题
                $filter->like('title', '标题');

                // 分类
                $tempVcattags = Vcattag::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vcattags', function ($query) use ($input) {
                        $query->where('vcattag_id', 'like', "{$input}");
                    });
                }, '分类')->select($tempVcattags);

                // 演员
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('actors', function ($query) use ($input) {
                        $query->where('actors.name', 'like', "%{$input}%");
                    });
                }, '演员');

                // 导演
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('directors', function ($query) use ($input) {
                        $query->where('directors.name', 'like', "%{$input}%");
                    });
                }, '导演');

                // 首播时间
                $filter->between('firstdate', '首播时间')->datetime();

            });
        });
    }


    protected function online_grid(Request $request)
    {
        return Admin::grid(Iaasset::class, function (Grid $grid) use ($request) {

            // 状态过滤
            // Publication::statusFilter($request->input('status'), $request->input('published'), $grid);
            $grid->model()->where('moderate_status', '=', 3)->where('online_status', '!=', 1);

            // 表格中的按钮
            $grid->actions(function ($actions) {

                $id                  = $actions->getKey();
                $url = route('entity.asset.audio.iaassets.iaasset_details', ['iaasset_id' => $id]);
                $actions->prepend('<a href ="' . $url . '" title="剧集详情"><i class="fa fa-list"></i></a>');

                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                //if (Admin::user()->cannot('entity.asset.audio.iaassets.destroy')) {
                $actions->disableDelete();
                //}

                // 提交上线审核申请
                if ( Admin::user()->can('content_audit.content_audits.create.online')) {
                    $url      = route('entity.content_audit.content_audits.create.online');
                    $function = "upAndDownByOne('$url', 'post', {content_id: $id, content_model: 'iaasset',audit_type: 'online', _token: LA.token})";
                    $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="提交上线申请"><i class="fa fa-eye"></i></a>');
                }

            });

            //添加按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.create')) {
            $grid->disableCreation();
            //}

            //导出按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.export')) {
            $grid->disableExport();
            //}

            // 批量操作
            $grid->tools(function ($tools) {
                // 批量按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('iaasset_no', '编号')->editable();
            $grid->column('name', '名称')->editable();
            $grid->column('iaasset_type', '剧集类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">单集</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-green">分集总集</span>';
                        break;
                }
            })->sortable();
            $grid->column('title', '标题')->editable();
            // $grid->column('subtitle', '副标题')->sortable();
            $grid->column('score', '评分')->editable()->label('default');
            $grid->column('season', '第几季')->editable();
            $grid->column('serials', '集数')->editable();
            $grid->column('upto', '更新到')->editable();
            $grid->column('vcat.name', '归类')->label('info');
            $grid->vcattags('分类')->pluck('name')->label();
            $grid->tags('标签')->pluck('name')->label('primary');
            $grid->vyears('年代')->pluck('name')->label('default');
            $grid->vareas('地区')->pluck('name')->label('info');
            $grid->vlangs('语言')->pluck('name')->label();
            $grid->column('firstdate', '首播日期')->display(function ($val) {
                if ($val) {
                    return date('Y-m-d', strtotime($val));
                }
                return '';
            })->sortable();
            // $grid->vifiles('文件资源地址')->pluck('name')->label();
            // $grid->vposteris('图片')->pluck('name')->label();
            // $grid->column('short_desc', '简介')->sortable();
            // $grid->column('long_desc', '详情')->sortable();
            // $grid->sps('服务商(SP)')->pluck('name')->label();
            // $grid->cps('内容商(CP)')->pluck('name')->label();
            // $grid->directors('导演')->pluck('name')->label();
            // $grid->actors('演员')->pluck('name')->label();
            $grid->column('charge_type', '收费类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">免费</span>';
                        break;

                    case 1:
                        return '<span class="badge bg-red">收费</span>';
                        break;
                }
            });
            $grid->column('listorder', '排序')->sortable()->editable();

            $grid->column('online_status', '上线审核')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();

            /**
             * 过滤
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();

                //评分
                $filter->between('score', '评分');

                // 归类
                $tempVcats = Vcat::pluck('name', 'id');
                $filter->equal('vcat_id', '归类')->select($tempVcats);

                // 语言
                $tempVlangs = Vlang::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vlangs', function ($query) use ($input) {
                        $query->where('vlang_id', '=', "{$input}");
                    });

                }, '语言')->select($tempVlangs);

                // 地区
                $tempVareas = Varea::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vareas', function ($query) use ($input) {
                        $query->where('varea_id', '=', "{$input}");
                    });

                }, '地区')->select($tempVareas);

                // 服务商(SP)
                $tempSps = Sp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('sps', function ($query) use ($input) {
                        $query->where('sp_id', '=', "{$input}");
                    });

                }, '服务商')->select($tempSps);

                // 内容商(CP)
                $tempCps = Cp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('cps', function ($query) use ($input) {
                        $query->where('cp_id', '=', "{$input}");
                    });

                }, '内容商')->select($tempCps);

                // 编号
                $filter->like('iaasset_no', '编号');

                // 标题
                $filter->like('title', '标题');

                // 分类
                $tempVcattags = Vcattag::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vcattags', function ($query) use ($input) {
                        $query->where('vcattag_id', 'like', "{$input}");
                    });
                }, '分类')->select($tempVcattags);

                // 演员
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('actors', function ($query) use ($input) {
                        $query->where('actors.name', 'like', "%{$input}%");
                    });
                }, '演员');

                // 导演
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('directors', function ($query) use ($input) {
                        $query->where('directors.name', 'like', "%{$input}%");
                    });
                }, '导演');

                // 首播时间
                $filter->between('firstdate', '首播时间')->datetime();

            });
        });
    }


    protected function publish_grid(Request $request)
    {
        return Admin::grid(Iaasset::class, function (Grid $grid) use ($request) {

            // 状态过滤
            // Publication::statusFilter($request->input('status'), $request->input('published'), $grid);
            $grid->model()->where('online_status', '=', 3)->where('publish_status', '!=', 1);

            // 表格中的按钮
            $grid->actions(function ($actions) {

                $id                  = $actions->getKey();
                $url = route('entity.asset.audio.iaassets.iaasset_details', ['iaasset_id' => $id]);
                $actions->prepend('<a href ="' . $url . '" title="剧集详情"><i class="fa fa-list"></i></a>');

                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                //if (Admin::user()->cannot('entity.asset.audio.iaassets.destroy')) {
                $actions->disableDelete();
                //}

                // 提交发布审核申请
                if ( Admin::user()->can('content_audit.content_audits.create.publish')) {
                    $url      = route('entity.content_audit.content_audits.create.publish');
                    $function = "upAndDownByOne('$url', 'post', {content_id: $id, content_model: 'iaasset',audit_type: 'publish', _token: LA.token})";
                    $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="提交发布申请"><i class="fa fa-eye"></i></a>');
                }

            });

            //添加按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.create')) {
            $grid->disableCreation();
            //}

            //导出按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.export')) {
            $grid->disableExport();
            //}

            // 批量操作
            $grid->tools(function ($tools) {
                // 批量按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('iaasset_no', '编号')->editable();
            $grid->column('name', '名称')->editable();
            $grid->column('iaasset_type', '剧集类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">单集</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-green">分集总集</span>';
                        break;
                }
            })->sortable();
            $grid->column('title', '标题')->editable();
            // $grid->column('subtitle', '副标题')->sortable();
            $grid->column('score', '评分')->editable()->label('default');
            $grid->column('season', '第几季')->editable();
            $grid->column('serials', '集数')->editable();
            $grid->column('upto', '更新到')->editable();
            $grid->column('vcat.name', '归类')->label('info');
            $grid->vcattags('分类')->pluck('name')->label();
            $grid->tags('标签')->pluck('name')->label('primary');
            $grid->vyears('年代')->pluck('name')->label('default');
            $grid->vareas('地区')->pluck('name')->label('info');
            $grid->vlangs('语言')->pluck('name')->label();
            $grid->column('firstdate', '首播日期')->display(function ($val) {
                if ($val) {
                    return date('Y-m-d', strtotime($val));
                }
                return '';
            })->sortable();
            // $grid->vifiles('文件资源地址')->pluck('name')->label();
            // $grid->vposteris('图片')->pluck('name')->label();
            // $grid->column('short_desc', '简介')->sortable();
            // $grid->column('long_desc', '详情')->sortable();
            // $grid->sps('服务商(SP)')->pluck('name')->label();
            // $grid->cps('内容商(CP)')->pluck('name')->label();
            // $grid->directors('导演')->pluck('name')->label();
            // $grid->actors('演员')->pluck('name')->label();
            $grid->column('charge_type', '收费类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">免费</span>';
                        break;

                    case 1:
                        return '<span class="badge bg-red">收费</span>';
                        break;
                }
            });
            $grid->column('listorder', '排序')->sortable()->editable();

            $grid->column('publish_status', '发布审核')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();

            /**
             * 过滤
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();

                //评分
                $filter->between('score', '评分');

                // 归类
                $tempVcats = Vcat::pluck('name', 'id');
                $filter->equal('vcat_id', '归类')->select($tempVcats);

                // 语言
                $tempVlangs = Vlang::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vlangs', function ($query) use ($input) {
                        $query->where('vlang_id', '=', "{$input}");
                    });

                }, '语言')->select($tempVlangs);

                // 地区
                $tempVareas = Varea::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vareas', function ($query) use ($input) {
                        $query->where('varea_id', '=', "{$input}");
                    });

                }, '地区')->select($tempVareas);

                // 服务商(SP)
                $tempSps = Sp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('sps', function ($query) use ($input) {
                        $query->where('sp_id', '=', "{$input}");
                    });

                }, '服务商')->select($tempSps);

                // 内容商(CP)
                $tempCps = Cp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('cps', function ($query) use ($input) {
                        $query->where('cp_id', '=', "{$input}");
                    });

                }, '内容商')->select($tempCps);

                // 编号
                $filter->like('iaasset_no', '编号');

                // 标题
                $filter->like('title', '标题');

                // 分类
                $tempVcattags = Vcattag::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vcattags', function ($query) use ($input) {
                        $query->where('vcattag_id', 'like', "{$input}");
                    });
                }, '分类')->select($tempVcattags);

                // 演员
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('actors', function ($query) use ($input) {
                        $query->where('actors.name', 'like', "%{$input}%");
                    });
                }, '演员');

                // 导演
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('directors', function ($query) use ($input) {
                        $query->where('directors.name', 'like', "%{$input}%");
                    });
                }, '导演');

                // 首播时间
                $filter->between('firstdate', '首播时间')->datetime();

            });
        });
    }


    protected function offline_grid(Request $request)
    {
        return Admin::grid(Iaasset::class, function (Grid $grid) use ($request) {

            // 状态过滤
            // Publication::statusFilter($request->input('status'), $request->input('published'), $grid);
            $grid->model()->where('online_status', '=', 3);

            // 表格中的按钮
            $grid->actions(function ($actions) {

                $id                  = $actions->getKey();
                $url = route('entity.asset.audio.iaassets.iaasset_details', ['iaasset_id' => $id]);
                $actions->prepend('<a href ="' . $url . '" title="剧集详情"><i class="fa fa-list"></i></a>');

                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.audio.iaassets.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                //if (Admin::user()->cannot('entity.asset.audio.iaassets.destroy')) {
                $actions->disableDelete();
                //}


                // 提交下线审核申请
                if ( Admin::user()->can('content_audit.content_audits.create.offline')) {
                    $url      = route('entity.content_audit.content_audits.create.offline');
                    $function = "upAndDownByOne('$url', 'post', {content_id: $id, content_model: 'iaasset',audit_type: 'offline', _token: LA.token})";
                    $actions->append('<a href="javascript:void(0)" onclick="' . $function . '" title="提交下线申请"><i class="fa fa-eye"></i></a>');
                }

            });

            //添加按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.create')) {
            $grid->disableCreation();
            //}

            //导出按钮
            //if (Admin::user()->cannot('entity.asset.audio.iaassets.export')) {
            $grid->disableExport();
            //}

            // 批量操作
            $grid->tools(function ($tools) {
                // 批量按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('iaasset_no', '编号')->editable();
            $grid->column('name', '名称')->editable();
            $grid->column('iaasset_type', '剧集类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">单集</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-green">分集总集</span>';
                        break;
                }
            })->sortable();
            $grid->column('title', '标题')->editable();
            // $grid->column('subtitle', '副标题')->sortable();
            $grid->column('score', '评分')->editable()->label('default');
            $grid->column('season', '第几季')->editable();
            $grid->column('serials', '集数')->editable();
            $grid->column('upto', '更新到')->editable();
            $grid->column('vcat.name', '归类')->label('info');
            $grid->vcattags('分类')->pluck('name')->label();
            $grid->tags('标签')->pluck('name')->label('primary');
            $grid->vyears('年代')->pluck('name')->label('default');
            $grid->vareas('地区')->pluck('name')->label('info');
            $grid->vlangs('语言')->pluck('name')->label();
            $grid->column('firstdate', '首播日期')->display(function ($val) {
                if ($val) {
                    return date('Y-m-d', strtotime($val));
                }
                return '';
            })->sortable();
            // $grid->vifiles('文件资源地址')->pluck('name')->label();
            // $grid->vposteris('图片')->pluck('name')->label();
            // $grid->column('short_desc', '简介')->sortable();
            // $grid->column('long_desc', '详情')->sortable();
            // $grid->sps('服务商(SP)')->pluck('name')->label();
            // $grid->cps('内容商(CP)')->pluck('name')->label();
            // $grid->directors('导演')->pluck('name')->label();
            // $grid->actors('演员')->pluck('name')->label();
            $grid->column('charge_type', '收费类型')->display(function ($val) {
                switch ($val) {
                    case 0:
                        return '<span class="badge bg-green">免费</span>';
                        break;

                    case 1:
                        return '<span class="badge bg-red">收费</span>';
                        break;
                }
            });
            $grid->column('listorder', '排序')->sortable()->editable();

            $grid->column('id', '下线审核')->display(function ($val) {
                $status = content_audit::audit_status($val,'iaasset','offline');
                switch ($status) {
                    case 0:
                        return '<span class="badge bg-blue">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 1:
                        return '<span class="badge bg-yellow">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 2:
                        return '<span class="badge bg-red">'.content_audit::$audit_status[$val].'</span>';
                        break;
                    case 3:
                        return '<span class="badge bg-green">'.content_audit::$audit_status[$val].'</span>';
                        break;
                }
            })->sortable();

            /**
             * 过滤
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();

                //评分
                $filter->between('score', '评分');

                // 归类
                $tempVcats = Vcat::pluck('name', 'id');
                $filter->equal('vcat_id', '归类')->select($tempVcats);

                // 语言
                $tempVlangs = Vlang::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vlangs', function ($query) use ($input) {
                        $query->where('vlang_id', '=', "{$input}");
                    });

                }, '语言')->select($tempVlangs);

                // 地区
                $tempVareas = Varea::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vareas', function ($query) use ($input) {
                        $query->where('varea_id', '=', "{$input}");
                    });

                }, '地区')->select($tempVareas);

                // 服务商(SP)
                $tempSps = Sp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('sps', function ($query) use ($input) {
                        $query->where('sp_id', '=', "{$input}");
                    });

                }, '服务商')->select($tempSps);

                // 内容商(CP)
                $tempCps = Cp::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('cps', function ($query) use ($input) {
                        $query->where('cp_id', '=', "{$input}");
                    });

                }, '内容商')->select($tempCps);

                // 编号
                $filter->like('iaasset_no', '编号');

                // 标题
                $filter->like('title', '标题');

                // 分类
                $tempVcattags = Vcattag::pluck('name', 'id');
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('vcattags', function ($query) use ($input) {
                        $query->where('vcattag_id', 'like', "{$input}");
                    });
                }, '分类')->select($tempVcattags);

                // 演员
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('actors', function ($query) use ($input) {
                        $query->where('actors.name', 'like', "%{$input}%");
                    });
                }, '演员');

                // 导演
                $filter->where(function ($query) {
                    $input = $this->input;
                    $query->whereHas('directors', function ($query) use ($input) {
                        $query->where('directors.name', 'like', "%{$input}%");
                    });
                }, '导演');

                // 首播时间
                $filter->between('firstdate', '首播时间')->datetime();

            });
        });
    }




}
